<template>
  <div class="bg">
    <el-card class="box-card">
      <el-row type="flex" class="row-bg" justify="end">
        <!-- <el-col :span="4" :offset="24">
          <el-input style="width:300px" placeholder="请输入商家名称/联系人/手机号码查询"></el-input>
        </el-col>-->
        <el-col :span="4" :offset="24">
          <el-input style="width:300px" v-model="selectCode" placeholder="请输入礼品名称查询"></el-input>
        </el-col>
        <!-- <el-col :span="4" :offset="1">
          <el-date-picker v-model="value1" type="date" placeholder="选择日期"></el-date-picker>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-date-picker v-model="value1" type="date" placeholder="选择日期"></el-date-picker>
        </el-col>-->
        <el-col :span="4" :offset="1">
          <el-select v-model="status" placeholder="状态" style="width:200px">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="4" :offset="1">
          <div style="display:flex">
            <el-button type="primary" @click="searchMessage">查询</el-button>
            <el-button type="success" @click="reset">重置</el-button>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 表格展示 -->
    <el-card class="card">
      <div slot="header">
        <el-row type="flex" justify="end">
          <el-col :span="2">
            <el-button type="success" @click="addcommodity">添加礼品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格展示 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column fixed prop="index" width="100" label="序号" align="center"></el-table-column>
        <el-table-column prop="goodsName" label="商品名称" align="center"></el-table-column>
        <el-table-column prop="name" label="礼品图片" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 80px" :src="scope.row.goodsImage"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="礼品效果图" align="center">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 80px" :src="scope.row.goodsDynamicFigure"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="province" label="礼品类型" align="center">
          <template slot-scope="scope">
            <span>{{scope.row.goodsType==1?'金币':'口粮'}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="goodsPrice" label="售价" align="center"></el-table-column>
        <el-table-column prop="goodsPresentValue" label="礼品价值" align="center"></el-table-column>
        <el-table-column prop="goodsShelfTime" label="添加时间" align="center"></el-table-column>
        <el-table-column prop="goodsStatus" label="状态" align="center">
          <template slot-scope="scope">
            <span>{{ giftStatus(scope.row.goodsStatus)}}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="220" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="redact(scope.row)">编辑</el-button>
            <el-button
              type="warning"
              size="small"
              @click="Standup(scope.row)"
            >{{scope.row.goodsStatus==1?'下架':'上架'}}</el-button>
            <el-button type="danger" size="small" @click="GiftDelet(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        :total="total"
        style="margin-top:24px"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :current-page="current"
      ></el-pagination>
    </el-card>

    <!-- 弹窗信息数据回显 -->
    <el-dialog title="编辑礼品" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <!-- 弹窗 -->
      <dialogs
        @addruleForm="newruleForm"
        @close="close"
        :goodsMsg="goodsMsg"
        :fileList="fileList"
        :fileListGift="fileListGift"
      />
    </el-dialog>

    <!-- 弹窗信息数据回显 -->
    <el-dialog
      title="添加礼品"
      :visible.sync="NewDialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <!-- 弹窗 -->
      <Newdynamic @addruleForm="newruleForm" :addstatus="addstatus" ref="mychild" @popup="popup" />
    </el-dialog>
  </div>
</template>

<script>
import { export2Excel } from "@/components/commom.js";
import dialogs from "@/components/dialog";
import Newdynamic from "@/components/Newdynamic";
export default {
  name: "parent",
  components: {
    dialogs,
    Newdynamic
  },
  data() {
    return {
      // 1 有效 2无效 3删除 4 待上架
      options: [
        {
          value: 1,
          label: "在售"
        },
        {
          value: 2,
          label: "已下架"
        },
        {
          value: 4,
          label: "待上架"
        }
      ],
      status: "",
      value1: "",
      downloadLoading: false,
      dialogVisible: false, //数据回显信息数据
      NewDialogVisible: false, //数据回显信息数据
      tableData: [],
      successrule: {},
      // 是否是新增和修改
      title: 1,
      total: 0, //总条数
      selectCode: "", //模糊查询
      current: 1, //分页

      /******测试数据******/
      goodsMsg: {},
      addstatus: false,
      // 回显动态图片
      fileListGift: [],
      fileList: [] //普通图片
    };
  },
  mounted() {
    this.giftlist();
  },
  methods: {
    //获取新增礼品列表
    giftlist() {
      let param = {
        size: 10,
        current: this.current,
        selectCode: this.selectCode,
        status: this.status
      };
      this.$http.article.giftlist(param).then(res => {
        this.tableData = res.result.records;
        this.total = res.result.total;
        console.log("获取新增列表", res);
        //显示每页展示的数量
        res.result.records.forEach((item, index) => {
          //res.data.data.list 表格数据     forEach为每一项添加数据
          item.index = index + 1 + (this.current - 1) * 10;
          // this.currentPage当前页  this.pageParams.size 每页显示多少    :page-sizes="[10,20,30,50]" 分页大小

          return item;
        });
      });
    },
    // 关闭
    close(val) {
      if (val) this.dialogVisible = false;
      console.log("打印子组件的参数", val);
    },
    // 关闭新建弹窗
    popup(val) {
      if (val) this.NewDialogVisible = false;
      console.log("关闭新建弹窗", val);
    },
    // 礼品状态
    giftStatus(val) {
      switch (val) {
        case 1:
          return "在售";
          nzsry;
          break;
        case 2:
          return "已下架";
          break;
        case 3:
          return "无效";
          break;
        case 4:
          return "待上架";
          break;
      }
    },
    // 获取上传图片
    Image(val) {
      console.log("获取静态图片", val);
      this.ruleForm.goodsImage = val;
    },
    // 获取动态图片
    goodsDynamic(val) {
      console.log("获取动态图片", val);
      this.ruleForm.goodsDynamicFigure = val;
    },
    // 分页
    handleCurrentChange(val) {
      this.current = val;
      this.giftlist();
    },
    // 条数
    handleSizeChange(val) {
      console.log("条数分页", val);
    },
    // 查询信息
    searchMessage() {
      this.current = 1; //模糊查询
      this.giftlist();
    },
    // 重置查询信息
    reset() {
      this.selectCode = "";
      this.status = "";
      this.giftlist();
    },
    // 上下架数据
    Standup(val) {
      console.log("获取上下架数据", val);
      let titleMessage = "";
      if (val.goodsStatus == 1) {
        titleMessage = "是否确定下架";
      } else {
        titleMessage = "是否上架";
      }

      this.$confirm(`此操作${titleMessage}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let status = 0;
          if (val.goodsStatus == 1) {
            status = 2;
          } else {
            status = 1;
          }
          let param = {
            goodsId: val.goodsId,
            goodsStatus: status
          };
          this.$http.article.addgift(param).then(res => {
            if (res.code == 200) {
              this.giftlist();
              this.$message({
                type: "success",
                message: "请求成功"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消成功"
          });
        });
    },
    // 删除商品列表数据
    GiftDelet(val) {
      console.log("修改商品列表", val);

      this.$confirm("是否确认删除？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          // var status = 0;
          // if (
          //   val.goodsStatus == 1 ||
          //   val.goodsStatus == 4 ||
          //   val.goodsStatus == 2
          // ) {
          //   status = 3;
          // }
          let param = {
            goodsId: val.goodsId,
            goodsStatus: 3
          };
          console.log("修改状态", param, "初始化状态", val.goodsStatus);
          this.$http.article.addgift(param).then(res => {
            if (res.code == 200) {
              if (res.code == 200) this.$message.success("删除成功");
              /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

              //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
              const totalPage = Math.ceil((this.total - 1) / 10);

              console.log("totalPage", totalPage);

              //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
              const pagenum =
                this.current > totalPage ? totalPage : this.current;

              console.log("获取总条数", pagenum);

              //避免pagenum变为0
              this.current = pagenum < 1 ? 1 : pagenum;

              this.giftlist();
            }
          });

          // this.$http.article
          //   .deleteGift({ petGoodsId: val.goodsId })
          //   .then(res => {
          //     if (res.code == 200) this.$message.success("删除成功");
          //     /*--------删除成功之后获取上一页的数据,避免是否为空--------*/

          //     //此时已经实现删除操作，所以total的值需要减size，Math.ceil是向上取整，保证始终大于等于0
          //     const totalPage = Math.ceil((this.total - 1) / 10);

          //     console.log("totalPage", totalPage);

          //     //将当前页码与删除数据之后的总页数进行比较，避免当前页码不存在
          //     const pagenum =
          //       this.current > totalPage ? totalPage : this.current;

          //     console.log("获取总条数", pagenum);

          //     //避免pagenum变为0
          //     this.current = pagenum < 1 ? 1 : pagenum;

          //     this.giftlist();
          //   });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    // 新增添加礼品
    newruleForm(val) {
      this.$http.article.addgift(val).then(res => {
        if (res.code == 200) this.NewDialogVisible = false;
        this.dialogVisible = false;
        this.giftlist();
        console.log("新增礼品数据", res);
      });
    },
    //   添加礼品弹窗
    addcommodity() {
      // console.log(this.addstatus);
      this.$nextTick(() => {
        this.$refs.mychild.childClick("我是子组件里面的方法");
      });
      // this.addstatus = true; //获取新的数据
      this.NewDialogVisible = true;
    },
    // 编辑查看
    redact(val) {
      console.log("查看编辑", val);
      this.fileListGift = [];
      this.fileList = [];
      // 获取礼品详情列表
      this.$http.article.getgift({ petGoodsId: val.goodsId }).then(res => {
        this.goodsMsg = res.result;
        this.fileListGift.push({ url: res.result.goodsDynamicFigure });
        this.fileList.push({ url: res.result.goodsImage });
        // this.ruleForm.goodsImage=res.result.goodsImage;
        console.log("获取礼品详情列表", res);
      });

      // this.successrule = val;
      // // 查看编辑
      this.dialogVisible = true;
      this.title = 2; //修改礼品
    },
    // 是否关闭弹窗
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.giftlist();
          done();
        })
        .catch(_ => {});
    }
  }
};
</script>

<style lang="scss" scoped>
.el-upload-list__item {
  transition: none !important;
}

.el-upload-list--picture-card .el-upload-list__item-actions {
  transition: none !important;
}
.merchantData {
  display: flex;
  align-items: center;
  padding-top: 14px;
  .addmerchant {
    color: #409eff;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 400;
  }
  .merchantNum {
    color: #f23a3a;
    font-size: 18px;
    font-family: Microsoft YaHei;
    font-weight: 800;
  }
}

.clearfix {
  padding-bottom: 20px;
}
.bg {
  width: 100%;
  height: 100%;
  background-color: #f4f7fd;
  padding-top: 10px;
}
.box-card {
  width: 98%;
  height: 80px;
  margin: 0 auto;
}
.card {
  width: 98%;
  margin: 0 auto;
  margin-top: 10px;
}
</style>